//
//  RXFrameTabBarViewController.m
//  RXFrame
//
//  Created by srxboys on 2021/4/20.
//

#import "RXFrameTabBarViewController.h"

#import "RXFrameViewController.h"
#import "RXFrameNavViewController.h"

@interface RXFrameTabBarViewController ()<UITabBarControllerDelegate>
@property (nonatomic, strong) NSDate *lastDate;
@end

@implementation RXFrameTabBarViewController
@synthesize current = _current;


//改变 控制器的 状态栏的 颜色
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

- (BOOL)shouldAutorotate
{
    return [self.selectedViewController shouldAutorotate];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return [self.selectedViewController supportedInterfaceOrientations];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
//    NSLog(@"self.tabbar=%@", self.tabBar);
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.delegate = self;
    /// 自定义 tabBarItem
    [self addNavsToTabBar];
    /// 标签栏 标题的 颜色
//    [self toTabBarFontColor];
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)tabBarHorder {
    //(iPhone/iPad)横屏 TabBarItem 显示的图片
    NSArray * itemArray = self.tabBar.items;
    for (NSInteger i = 0; i < itemArray.count; i ++) {
        NSString * imageName = [NSString stringWithFormat:@"tab_%zd_n", i];
        UITabBarItem * item = self.tabBar.items[i];
        item.landscapeImagePhone = [UIImage imageNamed:imageName];
        UIEdgeInsets inset = UIEdgeInsetsMake(-10, -10, 10, 10);
        item.landscapeImagePhoneInsets = inset;
        if (@available(iOS 11.0, *)) {
            item.largeContentSizeImage = [UIImage imageNamed:imageName];
            item.largeContentSizeImageInsets = inset;
        } else {

        }
    }
}

- (void)addNavsToTabBar {
//    self.home = [[SCMHomeViewController alloc] init];
//    _rootViewController = [self setNavRootViewControll:self.home titleStr:Localized(LOCAL_TAB_HOME) imagePath:@"tab_0_n" selectedImagePath:@"tab_0_s"];
//
//    self.func = [[SCMFuncViewController alloc] init];
//    [self setNavRootViewControll:self.func titleStr:Localized(LOCAL_TAB_FUNC) imagePath:@"tab_1_n" selectedImagePath:@"tab_1_s"];
//
//    self.news = [[SCMNewsViewController alloc] init];
//    [self setNavRootViewControll:self.news titleStr:Localized(LOCAL_TAB_NEWS) imagePath:@"tab_2_n" selectedImagePath:@"tab_2_s"];
//
//    self.profile = [[SCMProfileViewController alloc] init];
//    [self setNavRootViewControll:self.profile titleStr:Localized(LOCAL_TAB_PROFILE) imagePath:@"tab_3_n" selectedImagePath:@"tab_3_s"];
}

/**
 * @author ----------, 16-05-03 13:03:01
 *
 * @brief 设置viewController并添加到UITabBarViewControll
 *
 * @param viewController    VC控制器
 * @param title             tab标题、nav标题
 * @param imagePath         默认图片
 * @param selectedImagePath 选中、点击 是的图片
 */
- (RXFrameNavViewController *)setNavRootViewControll:(RXFrameViewController *)viewController titleStr:(NSString *)title imagePath:(NSString *)imagePath selectedImagePath:(NSString *)selectedImagePath {

    // 控制器放到 导航栏控制器中
    RXFrameNavViewController * nav = [[RXFrameNavViewController alloc] initWithRootViewController:viewController];
    nav.title = title;

    // tabBar图片渲染有个默认的值，改成UIImageRenderingModeAlwaysOriginal，就是图片本身图，否则会渲染【tint.color  灰--蓝(色)】
    UIImage * image = [[UIImage imageNamed:imagePath] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    UIImage * selectImage = [[UIImage imageNamed:selectedImagePath] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    // viewControll导航栏标题
    viewController.navigationItem.title = title;

    // 标签栏的标题
    viewController.tabBarItem.title = title;
    // 标签栏默认图片
    viewController.tabBarItem.image = image;
    // 标签栏选中时的图片
    viewController.tabBarItem.selectedImage = selectImage;

    // 导航栏放到 标签栏
    [self addChildViewController:nav];
    return nav;
}


- (void) toTabBarFontColor {
    // 默认颜色
    [UITabBarItem.appearance setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:RXColorRGB(66, 68, 80), NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];

    // 选中颜色
    [UITabBarItem.appearance setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:RXColorRGB(80, 10, 123), NSForegroundColorAttributeName,nil] forState:UIControlStateSelected];

//    [UITabBar.appearance setTintColor:[UIColor whiteColor]];
//    [UITabBar.appearance setAlpha:1];
}


// 点击tabBar上的ViewController时调用，为了以后
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    RXFrameNavViewController * navVC = (RXFrameNavViewController *)viewController;

    self.rootViewController = navVC;

//    RXLog(@"tabBar didSelect class=%@", NSStringFromClass([[navVC.viewControllers firstObject] class]));
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    RXFrameNavViewController * baseNavVC = self.rootViewController;
    /** 默认是YES
     * 点击的不是当前页面， 有需求判断用户登录时，没有登录，并不登录，就不展示页面
     * 返回的界面是 self.tempIndex
     *  return NO;
     */

    NSDate *date = [NSDate date];
    if (baseNavVC && [baseNavVC isEqual:viewController]) {
        if (date.timeIntervalSince1970 - _lastDate.timeIntervalSince1970 < 0.5) {
            RXFrameViewController * baseVC = [baseNavVC.viewControllers firstObject];
            [baseVC tabBarItemDoubleClick];
        }

        self.lastDate = date;
        return NO;
    }

    return YES;
}


- (void)setCurrent:(NSInteger)current {
    _current = current;
    self.selectedIndex = current;
    RXFrameNavViewController * nav = (RXFrameNavViewController *)self.viewControllers[current];
    nav.viewControllers = @[[nav.viewControllers firstObject]];
    self.rootViewController = nav;

}

- (NSInteger)current {
    _current = self.selectedIndex;
    return _current;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
